<?php
/*
 * by: PhpStorm
 * 2021/7/23
 * 21:39
 * User: jweicao@qq.com
 */
namespace App\Api\Controllers\activity;

use App\Api\Bases\BaseController;
use App\Api\Models\PromotionBargain;
use App\Api\Repositories\BargainBasic;
use App\Api\Models\PromotionBargainGoods;
use App\Api\Models\PromotionBargainList;
use App\Api\Models\PromotionBargainRecord;
use App\Api\Services\JsonService;
use App\Api\Services\OrderBasic;
use App\Models\setting\CmcSystemConfig;
use App\Models\setting\CmcSystemConfigTab;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use function Symfony\Component\String\s;

/**
 * 砍价活动
 * Class BargainController
 * @package App\Api\Controllers\activity
 */
class BargainController extends BaseController
{

    /**
     * 砍价活动列表
     * @return array
     */
    public function BargainsList()
    {
        $field = [
            'id','bargain_name','bargain_type'
        ];
        $data = PromotionBargainGoods::bargainLists($field);

        if(!$data) return JsonService::fail('没有数据');
        return JsonService::success('ok',$data);
    }

    /**
     * 用户已经有砍价商品
     * @param Request $request
     * @return array
     */
    public function userBargainList(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
            ]);
            $error = $params->errors()->first();
            return JsonService::fail($error);
        } catch (\Exception $exception) {
            return JsonService::fail($exception->getMessage());
        }
        $data = PromotionBargainList::userBargainGoods($params['member_id']);

        if(!$data) return JsonService::fail('没有数据');
        return JsonService::success('ok',$data);
    }

    /**
     * @param Request $request
     */
    public function bargainGoodsInfo(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
                'bargain_id' =>'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }
        $data = PromotionBargainList::barInfo($params);
        if(!$data) return JsonService::fail('没有数据');
        return JsonService::success('ok',$data);

    }
    /**
     * 自己初次砍价
     * @param Request $request
     * @return array
     */
    public function firstBar(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
                'bargain_id' =>'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }
        try {
            $endTime = PromotionBargain::field($params['bargain_id'],'end_time');
            if ($endTime < date('Y-m-d H:i:s',time())) return JsonService::fail('活动已经结束');

            $buyType = PromotionBargain::field($params['bargain_id'],'buy_type');
            if ($buyType<=0) return JsonService::fail('商品已经领完');
            $count = PromotionBargainList::barCount($params);
            if ($count) return JsonService::fail('您已参与过,无法再次参与');
            $result = BargainBasic::firstBar($params['member_id'],$params['bargain_id']);
            if (isset($result) && $result['code']== 200 ){
                return JsonService::success($result['msg'],$result['data']);
            }
            return JsonService::fail($result['msg'],$result['data']);
        }catch (\Exception $exception){
            return JsonService::fail($exception->getMessage());
        }

    }

    /**
     * 用户帮砍
     * @param Request $request
     * @return array
     */
    public function helpBar(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
                'bargain_id' =>'required|integer',
                'help_member_id' => 'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        try {
            $where = [
                'member_id' => $params['member_id'],
                'bargain_id' => $params['bargain_id'],
            ];
            //砍价开始时间
            $status = BargainBasic::oldTime($where);
            if (!$status) return JsonService::fail('该商品已超出砍价期限');

            $helpNum = PromotionBargainRecord::helpNum($where,$params['help_member_id']);
            if ($helpNum >= 1) return JsonService::fail('您的助力次数已达上限');

            $oldNum = PromotionBargainRecord::old_num($where);

            if ($oldNum > 1){
                $model = BargainBasic::helpBar($params['member_id'], $params['bargain_id'], $params['help_member_id']);
            } elseif ($oldNum == 1) {
                $model = BargainBasic::lastBar($params['member_id'], $params['bargain_id'], $params['help_member_id']);
                if ($model) return JsonService::success('该商品已经砍成,请尽快支付领取');
            }elseif($oldNum<=0){
                return JsonService::fail('该商品砍价剩余次数不足');
            }

            return JsonService::success('砍价成功', $model);
        }catch (\Exception $exception){
            return JsonService::fail($exception->getMessage());

        }
    }

    /**
     * 帮砍列表
     * @param Request $request
     * @return array
     */
    public function barRecord(Request $request){
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
                'bargain_id' =>'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }
        $field = [
            'member_id','money','nickname','headimg',
        ];

        $lists = PromotionBargainRecord::barRecord($params,$field);
        if (!$lists) return JsonService::fail('还没有好友帮砍');
        return JsonService::success('查询成功',$lists);
    }

    /**
     * 砍价订单表
     * @param Request $request
     * @return array
     */
    public function barOrder(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
                'bargain_id' =>'required|integer',
                'address_id' =>'required|integer',
                'total_money'=>'required|numeric'
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }
        $orderSn = OrderBasic::orderSn();
        $data = [
            'order_sn' =>$orderSn,
            'type' =>3,
            'u_id'=>$params['member_id'],
            'total_money' => $params['total_money'],
            'bargain_id' =>$params['bargain_id'],
            'address_id' => $params['address_id'],
            'order_time' => date('Y-m-d H:i:s',time()),

        ];

        $result = PromotionBargainList::saveData($data);
        if (!$result) return JsonService::fail('订单生成失败');
        return JsonService::success('订单生成成功',$result);
    }


}
